package com.leoman.plz_p.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.leoman.plz_p.constants.Constants;
import com.leoman.plz_p.module.bean.ExamSubmitRequestBean;
import com.leoman.plz_p.views.study.AnswerQuestionFragment;

import java.util.List;

/**
 * Created by guoh on 2018/7/7.
 */
public class QuestionPagerAdapter extends FragmentPagerAdapter {


    private List<String> questionIds;
    private AnswerQuestionFragment.onQuestionAnswerRight answerRight;
    private boolean showExplain;
    private List<ExamSubmitRequestBean> requestBeans;
    private boolean isNotNormalExam;

    public QuestionPagerAdapter(FragmentManager fm, List<String> questionIds, AnswerQuestionFragment.onQuestionAnswerRight questionAnswerRight, boolean showExplain, List<ExamSubmitRequestBean> requestBeans,boolean isNotNormalExam) {
        super(fm);
        this.questionIds=questionIds;
        this.answerRight=questionAnswerRight;
        this.showExplain=showExplain;
        this.requestBeans=requestBeans;
        this.isNotNormalExam =isNotNormalExam;
    }

    @Override
    public int getCount() {
        return questionIds.size();
    }


    @Override
    public Fragment getItem(int position) {
        AnswerQuestionFragment fragment=new AnswerQuestionFragment();
        Bundle bundle=new Bundle();
        bundle.putString(Constants.KEY_QUESTION_ID,questionIds.get(position));
        bundle.putBoolean(Constants.KEY_SHOW_EXPLAIN,showExplain);
        bundle.putBoolean(Constants.KEY_ANSWER_ERROR_SHOW_RIGHT, isNotNormalExam);
        if (requestBeans!=null&&requestBeans.size()>0){
            for (int i = 0; i <requestBeans.size() ; i++) {
                if (String.valueOf(requestBeans.get(i).getId()).equals(questionIds.get(position))){
                    bundle.putString(Constants.KEY_ANSWER,requestBeans.get(i).getChoice());
                }
            }
            bundle.putBoolean(Constants.KEY_HAS_ANSWER,true);
        }
        fragment.setArguments(bundle);
        fragment.setOnAnswerListener(answerRight);
        return fragment;
    }

}
